import { ref } from 'vue'

/**
 * 大小写转换功能组合式函数
 */
export function useCaseConverter() {
  const isProcessing = ref(false)

  // 转换为驼峰命名法
  const toCamelCase = (str: string): string => {
    return str
      .replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) => {
        return index === 0 ? word.toLowerCase() : word.toUpperCase()
      })
      .replace(/\s+/g, '')
      .replace(/[-_\.]/g, '')
  }

  // 转换为短横线命名法
  const toKebabCase = (str: string): string => {
    return str
      .replace(/([a-z])([A-Z])/g, '$1-$2')
      .replace(/[\s_\.]/g, '-')
      .toLowerCase()
      .replace(/-+/g, '-')
      .replace(/^-|-$/g, '')
  }

  // 转换为蛇形命名法
  const toSnakeCase = (str: string): string => {
    return str
      .replace(/([a-z])([A-Z])/g, '$1_$2')
      .replace(/[\s-\.]/g, '_')
      .toLowerCase()
      .replace(/_+/g, '_')
      .replace(/^_|_$/g, '')
  }

  // 转换为帕斯卡命名法
  const toPascalCase = (str: string): string => {
    return str
      .replace(/(?:^\w|[A-Z]|\b\w)/g, (word) => word.toUpperCase())
      .replace(/\s+/g, '')
      .replace(/[-_\.]/g, '')
  }

  // 转换为大写下划线命名法
  const toScreamingSnakeCase = (str: string): string => {
    return toSnakeCase(str).toUpperCase()
  }

  // 转换为点分隔命名法
  const toDotCase = (str: string): string => {
    return str
      .replace(/([a-z])([A-Z])/g, '$1.$2')
      .replace(/[\s-_]/g, '.')
      .toLowerCase()
      .replace(/\.+/g, '.')
      .replace(/^\.|\.$/g, '')
  }

  // 转换为全小写
  const toLowerCase = (str: string): string => {
    return str.toLowerCase()
  }

  // 转换为全大写
  const toUpperCase = (str: string): string => {
    return str.toUpperCase()
  }

  // 首字母大写
  const capitalize = (str: string): string => {
    return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase()
  }

  // 标题格式（每个单词首字母大写）
  const toTitleCase = (str: string): string => {
    return str.replace(/\w\S*/g, (txt) => {
      return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
    })
  }

  // 反转大小写
  const toggleCase = (str: string): string => {
    return str.replace(/[a-zA-Z]/g, (char) => {
      return char === char.toLowerCase() ? char.toUpperCase() : char.toLowerCase()
    })
  }

  // 转换函数映射
  const converters: Record<string, (str: string) => string> = {
    camelCase: toCamelCase,
    kebabCase: toKebabCase,
    snakeCase: toSnakeCase,
    pascalCase: toPascalCase,
    screamingSnakeCase: toScreamingSnakeCase,
    dotCase: toDotCase,
    toLowerCase: toLowerCase,
    toUpperCase: toUpperCase,
    capitalize: capitalize,
    titleCase: toTitleCase,
    toggleCase: toggleCase
  }

  // 主转换函数
  const convert = (type: string, text: string): string => {
    if (!text) return ''
    
    isProcessing.value = true
    
    try {
      const converter = converters[type]
      if (!converter) {
        throw new Error(`未知的转换类型: ${type}`)
      }
      
      // 按行处理文本
      const lines = text.split('\n')
      const convertedLines = lines.map(line => {
        if (!line.trim()) return line
        return converter(line)
      })
      
      return convertedLines.join('\n')
    } catch (error) {
      throw error
    } finally {
      isProcessing.value = false
    }
  }

  return {
    convert,
    isProcessing
  }
}